{include file="common/header" /}

<div class="panel panel-default panel-intro">
    {:build_heading()}

    <div class="panel-body">
        <div id="myTabContent" class="tab-content">
            <div class="tab-pane fade active in" id="one">
                <div class="widget-body no-padding">
                    <div id="toolbar" class="toolbar">
                        {:build_toolbar('refresh')}
                    </div>
                    <table id="table" class="table table-striped table-bordered table-hover table-nowrap"
                           data-operate-edit="{:$auth->check('scanwork/report/edit')}" 
                           data-operate-del="{:$auth->check('scanwork/report/del')}" 
                           width="100%">
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    var Table = $("#table").bootstrapTable({
        url: 'scanwork/report/activeReport',
        pk: 'id',
        sortName: 'id',
        sortOrder: 'desc',
        columns: [
            [
                {checkbox: true},
                {field: 'id', title: 'ID', width: 80},
                {field: 'order_no', title: '订单号', width: 150},
                {field: 'customer_name', title: '客户名称', width: 120},
                {field: 'product_name', title: '产品名称', width: 120},
                {field: 'model_name', title: '型号', width: 100},
                {field: 'process_name', title: '工序', width: 100},
                {field: 'user_name', title: '员工', width: 100},
                {field: 'report_type', title: '报工类型', width: 100, formatter: function(value) {
                    return value == 'piece' ? '计件' : '计时';
                }},
                {field: 'quantity', title: '数量', width: 80, formatter: function(value, row) {
                    return row.report_type == 'piece' ? value : '-';
                }},
                {field: 'total_hours', title: '工时', width: 80, formatter: function(value, row) {
                    return row.report_type == 'time' ? value + '小时' : '-';
                }},
                {field: 'piece_wage', title: '计件工资', width: 100, formatter: function(value, row) {
                    return row.report_type == 'piece' ? '¥' + value : '-';
                }},
                {field: 'time_wage', title: '计时工资', width: 100, formatter: function(value, row) {
                    return row.report_type == 'time' ? '¥' + value : '-';
                }},
                {field: 'piece_status', title: '计件状态', width: 100, formatter: function(value, row) {
                    if (row.report_type != 'piece') return '-';
                    var statusMap = {0: '待审核', 1: '已通过', 2: '已拒绝'};
                    var classMap = {0: 'warning', 1: 'success', 2: 'danger'};
                    return '<span class="label label-' + classMap[value] + '">' + statusMap[value] + '</span>';
                }},
                {field: 'time_status', title: '计时状态', width: 100, formatter: function(value, row) {
                    if (row.report_type != 'time') return '-';
                    var statusMap = {0: '待审核', 1: '已通过', 2: '已拒绝'};
                    var classMap = {0: 'warning', 1: 'success', 2: 'danger'};
                    return '<span class="label label-' + classMap[value] + '">' + statusMap[value] + '</span>';
                }},
                {field: 'work_date', title: '工作日期', width: 120},
                {field: 'createtime', title: '创建时间', width: 150, formatter: Table.api.formatter.datetime},
                {field: 'operate', title: __('Operate'), table: Table, events: Table.api.events.operate, 
                 buttons: [
                     {
                         name: 'audit',
                         text: __('审核'),
                         title: '审核主动报工',
                         classname: 'btn btn-xs btn-success btn-audit',
                         icon: 'fa fa-check',
                         url: 'scanwork/report/auditActiveReport',
                         callback: function (data) {
                             Layer.alert("审核成功");
                             Table.api.reload();
                         }
                     }
                 ],
                 formatter: Table.api.formatter.operate}
            ]
        ]
    });

    // 审核按钮事件
    $(document).on("click", ".btn-audit", function () {
        var ids = Table.api.selectedids();
        if (ids.length == 0) {
            Toastr.error("请选择要审核的记录");
            return;
        }
        if (ids.length > 1) {
            Toastr.error("一次只能审核一条记录");
            return;
        }
        
        var row = Table.api.getrowbyid(ids[0]);
        var status = row.report_type == 'piece' ? row.piece_status : row.time_status;
        
        if (status == 1) {
            Toastr.error("该记录已审核通过");
            return;
        }
        
        var auditType = row.report_type == 'piece' ? '计件' : '计时';
        var content = '<div class="form-group">' +
            '<label>审核结果：</label>' +
            '<div class="radio">' +
            '<label><input type="radio" name="status" value="1" checked> 通过</label>' +
            '<label><input type="radio" name="status" value="2"> 拒绝</label>' +
            '</div>' +
            '</div>' +
            '<div class="form-group">' +
            '<label>审核意见：</label>' +
            '<textarea name="audit_reason" class="form-control" rows="3" placeholder="请输入审核意见"></textarea>' +
            '</div>';
        
        Layer.open({
            type: 1,
            title: '审核' + auditType + '报工',
            area: ['500px', '300px'],
            content: '<form class="form-horizontal" style="padding: 20px;">' + content + '</form>',
            btn: ['确定', '取消'],
            yes: function (index, layero) {
                var status = layero.find('input[name="status"]:checked').val();
                var audit_reason = layero.find('textarea[name="audit_reason"]').val();
                
                Fast.api.ajax({
                    url: 'scanwork/report/auditActiveReport',
                    data: {
                        id: ids[0],
                        status: status,
                        audit_reason: audit_reason
                    }
                }, function (data, ret) {
                    Layer.close(index);
                    Table.api.reload();
                    return false;
                });
            }
        });
    });
</script>

{include file="common/footer" /} 